''' Mission 12 - Sounds Fun Race Day project, objective 9 ''' from codex import * from soundlib import * from time import sleep def screen_layout(): display.fill_rect(0, 0, 240, 30, LIGHT_GRAY) display.draw_text("RACE CONTROLLER", x=35, y=8, color=BLACK, scale=2) display.draw_text("MUSIC", x=20, y=90, color=WHITE, scale=3) display.draw_text("START", x=20, y=130, color=WHITE, scale=3) display.draw_text("FINISH", x=20, y=170, color=WHITE, scale=3) display.draw_text("WARNING", x=20, y=210, color=WHITE, scale=3) display.draw_rect(0, 80, 240, 40, GRAY) display.draw_rect(0, 120, 240, 40, GRAY) display.draw_rect(0, 160, 240, 40, GRAY) display.draw_rect(0, 200, 240, 40, GRAY) def menu_buttons(): global menu_index if buttons.was_pressed(BTN_U): menu_index = max(menu_index - 1, 0) elif buttons.was_pressed(BTN_D): menu_index = min(menu_index + 1, 3) def show_status(msg): display.fill_rect(0, 30, 240, 50, BLACK) display.draw_text(msg, 10, 50, color=YELLOW, scale=2) def start_race(): show_status("Start race...") for i in range(4): trumpet.set_pitch(440) trumpet.play() sleep(0.1) trumpet.stop() sleep(0.1) trumpet.set_pitch(880) trumpet.play() sleep(0.2) trumpet.stop() def toggle_music(): global is_playing is_playing = not is_playing if is_playing: show_status("Started music...") music_track.play(loop=True) else: show_status("Stopped music!") music_track.stop() def action_buttons(): if buttons.was_pressed(BTN_A): if menu_index == 0: toggle_music() elif menu_index == 1: start_race() elif menu_index == 2: show_status("Finish race...") elif menu_index == 3: show_status("Warning sound...") # --MAIN PROGRAM-- # Global variables menu_index = 0 menu_y = [80, 120, 160, 200] init = True show_status("Scroll=U/D Select=A") trumpet = soundmaker.get_tone('trumpet') music_track = soundmaker.get_mp3('sounds/funk', play=False) is_playing = False while True: prev_sel = menu_index menu_buttons() if menu_index != prev_sel or init: init = False display.fill_rect(0, menu_y[prev_sel], 240, 40, BLACK) display.fill_rect(0, menu_y[menu_index], 240, 40, DARK_BLUE) screen_layout() action_buttons()